<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * date: 2021-06-03 18:30:49
 * @LastEditors: Sam
 * @LastEditTime: 2022-05-15 23:43:22
 */


namespace app\__MODULE__\controller;
use app\__MODULE__\model\ Notice;
use app\__MODULE__\model\ NoticeType as NoticeTypeModel ;
use app\common\BaseController;

/**
 * 资讯分类
 * @author Sam
 * date 2021-11-03
 */
class NoticeType extends BaseController{
    
    /**
     * 资讯列表
     * @return think\Response\Json
     * @author Sam
     * @date 2021-11-03
     */
    public function index(){
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $wheres = [];
        if(isset($search['title']) && $search['title'] != '')  $wheres[] = ['title','like',"%{$search['title']}%"];  
        $query  = NoticeTypeModel::where($wheres);
        $total  = $query->count();
        $data   = $query->page($page, $limit)->order('sort desc')->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);
    }


    
            /**
     * 详情
     * @param int $id
     * @return \think\response\Json
     * @author Sam
     * @date 2022-04-26
     */
    public function detail(int $id): \think\response\Json
    {
        try {
            $row  = NoticeTypeModel::where('id', $id)->find();
        } catch (DataNotFoundException | ModelNotFoundException | DbException $e) {
            return $this->error($e->getMessage());
        }

        return $this->success('', $row);
    }
    /**
     * 添加
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function create(){
        $data = request()->post();
        try {
            $this->validate($data, [
                'title' => 'require',
            ], [
                'title.require'      => '标题不得为空',
            ]);
        } catch (\Exception $e) {
            json_response(0,$e->getError());
        }
        if(NoticeTypeModel::create($data)) json_response(1,'操作成功');
    }

    /**
     * 编辑
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function edit(){
        $data = request()->post();
        try {
            $this->validate($data, [
                'id' => 'require',
                'title' => 'require',
            ], [
               'id.require'  => 'ID必须',
               'title.require'      => '标题不得为空',
            ]);
        } catch (\Exception $e) {
            json_response(0,$e->getError());
        }
        if(NoticeTypeModel::update($data,['id'=>$data['id']])) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }


        /**
     * 删除
     * @return void
     * @author Sam
     * date 2021-06-04
     */
    public function delete(){
        $data = request()->post();
        try {
            $this->validate($data, [
                'ids' => 'require',
            ], [
                'ids.require'  => 'ID必须',
            ]);
        } catch (Exception $e) {
            json_response(0,$e->getError());
        }
        $ids = explode(',',$data['ids']);
        if(!$NoticeTypeModel = NoticeTypeModel::where('id','in',$ids)->select()) json_response(0,'不存在');
        if($NoticeTypeModel->delete()) json_response(1,'操作成功');
        json_response(0,'操作失败');
    }
}